package SwingWorkers;

import CommonOperationsClientServer.FilesOperations;
import GUIClient.GUIExtraFunctions;
import GUIClient.GUIOperationInProgress;
import java.io.BufferedReader;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;

public class SwingWorkerDownloadFile extends SwingWorker<Boolean, Object> {

    GUIOperationInProgress progress = new GUIOperationInProgress();
    String directory;
    String file;
    BufferedReader in;
    JTextPane local;
    JTextPane server;

    public SwingWorkerDownloadFile(String directoryDestination, String fileName, BufferedReader input,
            JTextPane localPane, JTextPane serverPane) {
        directory = directoryDestination;
        file = fileName;
        in = input;
        local = localPane;
        server = serverPane;
    }

    @Override
    protected Boolean doInBackground() throws Exception {
        GUIExtraFunctions.updateTextPane(server, "Solicitud concedida. Enviando OK al cliente");
        GUIExtraFunctions.updateTextPane(local, "Solicitud de Descarga aceptada. Descargando fichero...");
        GUIExtraFunctions.updateTextPane(server, "Enviando fichero....");
        progress.setVisible(true);
        FilesOperations.receiveFile(directory, file, in);
        progress.setVisible(false);
        GUIExtraFunctions.updateTextPane(local, "Descarga del fichero finalizada");
        GUIExtraFunctions.updateTextPane(server, "Envío del fichero finalizado");
        return true;
    }
}
